package com.yc.biz.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.yc.bean.Good;
import com.yc.bean.Types;
import com.yc.biz.GoodBiz;
import com.yc.dao.DBHelper;

public class GoodBizImpl implements GoodBiz {

	private DBHelper db = new DBHelper();

	@Override
	public List<Good> findTypeByTid(int tid) {
		String sql="select * from good where tid="+tid;
		return db.find(sql, null, Good.class);
	}
	
	@Override//根据类别的主键查询类别名
	public Types findTnameByTid(int tid) {
		String sql="select tname from type where tid="+tid;
		List<Types> list=db.find(sql, null, Types.class);
		if(list.size()>0){
			return list.get(0);
		}else{
			return null;
		}
		
	}
	@Override
	public List<Map<String, String>> findGoodsByTid(int one, int two) {
		String sql="select * from good where tid in("+one+","+two+") order by tid";
		return db.findAll(sql, null);
	}

	@Override
	public Good findGoodGid(int gid) {
		String sql="select * from good where gid="+gid;
		List<Good> list=db.find(sql, null, Good.class);
		if(list.size()>0){
			return list.get(0);
		}else{
			return null;
		}
		
	}

	@Override
	public List<Map<String, String>> findGoodsByRand() {
		String sql="select * from good where tid in(1,2,3,4,7) order by rand() limit 5";
		return db.findAll(sql, null);
	}

	@Override
	public List<Map<String, String>> findUp() {
		String sql="select * from good where tid in(8,9) order by rand() limit 5";
		return db.findAll(sql, null);
	}

	
	@Override
	public List<Map<String, String>> findDown() {
		String sql="select * from good where tid in(6,10) order by rand() limit 4";
		return db.findAll(sql, null);
	}

	@Override//主页的模糊查询
	public List<Good> findSome(String str) {
		String sql="select * from good where tname like '%"+str+"%'";
		
		return db.find(sql, null, Good.class);
	}

	
	//后台
		@Override
		public List<Good> getGoodsByPage(int page) {
			String sql = "select * from good limit "+(page-1)*10+","+10;	//从第一行开始查找十条数据
			List<Good> list = db.find(sql, null, Good.class);
			return list;
		}

		@Override
		public int good_del(int gid) {
			String sql = "DELETE FROM good WHERE gid="+gid;
			return db.doUpdate(sql, null);
		}
		
		//获取商品总页面数
		@Override
		public int getGoodsTotalPage() {
			String sql = "SELECT (1) FROM good";
			int total = db.findAll(sql, null).size();
			int goodTotalPage = total/10+1;		//eg:33条数据,每一页10条,共需要4页.但33/10==4 so +1
			return goodTotalPage;
		}

		@Override
		public int good_add(int tid, String tname, String pic, double price, String color, String version, String message,
				int inventory) {
			String sql = "INSERT INTO good VALUES(0,?,?,?,?,?,?,?,?)";
			List<Object> params = new ArrayList<Object>();
			params.add(tid);
			params.add(tname);
			params.add(pic);
			params.add(price);
			params.add(color);
			params.add(version);
			params.add(message);
			params.add(inventory);
			return db.doUpdate(sql, params);
		}

		@Override
		public int good_modify(int gid, int tid, String gname,String pic, int inventory, double price, String color,
				String version, String message) {
			String sql = "UPDATE good SET tid=?,tname=?,pic=?,price=?,color=?,version=?,message=?,inventory=? WHERE gid=?";
			List<Object> params = new ArrayList<Object>();
			params.add(tid);
			params.add(gname);
			params.add(pic);
			params.add(price);
			params.add(color);
			params.add(version);
			params.add(message);
			params.add(inventory);
			params.add(gid);
			return db.doUpdate(sql, params);
		}

		
		
}
